package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppServiceOrder;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppServices;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppSmsService;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.mapper.DxshAppServiceOrderMapper;
import com.syni.mdd.sjb.common.component.utils.common.quartz.QuartzDo;
import com.syni.mdd.sjb.common.component.utils.common.spring.ApplicationContextHolder;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.utils.SnowflakeIdWorkerUtils;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppServiceOrderService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppServicesService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppSmsServiceService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.order.DxshAppServiceOrderRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.order.GenerateOrderRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.services.GetOrderIdRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.sms.SmsOrderRqVo;
import com.syni.mdd.sjb.common.datatreaure.listen.order.DataBankOrderListenerHandler;
import com.syni.mdd.sjb.common.datatreaure.listen.order.MddDataBankOrderEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 电享生活商户端APP 服务订单表 服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-04-27
 */
@Service
public class DxshAppServiceOrderServiceImpl extends ServiceImpl<DxshAppServiceOrderMapper, DxshAppServiceOrder> implements DxshAppServiceOrderService {

    @Autowired
    DxshAppServicesService dxshAppServicesService;
    @Autowired
    DxshAppSmsServiceService dxshAppSmsServiceService;

    @Autowired
    DxshAppServiceOrderMapper dxshAppServiceOrderMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> generateOrder(GenerateOrderRqVo generateOrderRqVo) {
        if(generateOrderRqVo.getServiceNum() < 0){
            return ResultMapHelper.failure("服务数量不能小于0");
        }
        DxshAppServices dxshAppServices = dxshAppServicesService.getById(generateOrderRqVo.getServiceId());
        if(dxshAppServices == null){
            return ResultMapHelper.failure(ResultMsg.MSG_MIN_APP_CREATE_FAIL);
        }
        DxshAppServiceOrder dxshAppServiceOrder = createOrder(generateOrderRqVo.getServiceId(),
                generateOrderRqVo.getServiceNum(),addedType, generateOrderRqVo.getPhone(), generateOrderRqVo.getUserId());
        if(dxshAppServiceOrder == null){
            return ResultMapHelper.failure(ResultMsg.CODE_ERROR_05);
        }
        DxshAppServiceOrderRpVo dxshAppServiceOrderRpVo = BeanUtils.copyProperties(dxshAppServiceOrder,DxshAppServiceOrderRpVo.class);
        //切入一个通知
        ApplicationContextHolder.getApplicationContext().
                publishEvent(new MddDataBankOrderEvent("generateOrderService",dxshAppServiceOrder,dxshAppServices));
        return ResultMapHelper.success(dxshAppServiceOrderRpVo, ResultMsg.HAND_SUCCESS);
    }

    @Override
    public Map<String, Object> generateSmsOrder(SmsOrderRqVo smsOrderRqVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppSmsService.ID,smsOrderRqVo.getSmsServiceId());
        queryWrapper.eq(DxshAppSmsService.IS_DELETE,Constants.COMMON_FLAG_FALSE_INT);
        DxshAppSmsService dxshAppSmsService = dxshAppSmsServiceService.getOne(queryWrapper);
        if(dxshAppSmsService == null){
            return ResultMapHelper.failure("找不到该短信编号");
        }
        DxshAppServiceOrder dxshAppServiceOrder = new DxshAppServiceOrder();
        dxshAppServiceOrder.setOrderNo(SnowflakeIdWorkerUtils.getOrderNo());
        dxshAppServiceOrder.setServiceId(smsOrderRqVo.getSmsServiceId());
        dxshAppServiceOrder.setOrderType(smsType);
        dxshAppServiceOrder.setBmsUserId(smsOrderRqVo.getUserId());
        dxshAppServiceOrder.setTotalPrice(dxshAppSmsService.getPrice());
        dxshAppServiceOrder.setNewTime(LocalDateTime.now());
        dxshAppServiceOrder.setUpdateTime(LocalDateTime.now());
        dxshAppServiceOrder.setCreateWay(APP);
        dxshAppServiceOrderMapper.insert(dxshAppServiceOrder);
        DxshAppServiceOrderRpVo dxshAppServiceOrderRpVo = BeanUtils.copyProperties(dxshAppServiceOrder,DxshAppServiceOrderRpVo.class);
        return ResultMapHelper.success(dxshAppServiceOrderRpVo, ResultMsg.HAND_SUCCESS);
    }

    @Override
    public List<DxshAppServiceOrder> onlineAddTypeOrder() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppServiceOrder.IS_DELETE,Constants.COMMON_FLAG_FALSE_INT);
        queryWrapper.eq(DxshAppServiceOrder.ORDER_TYPE,Constants.addedType);
        queryWrapper.eq(DxshAppServiceOrder.PAY_STATUS,new HashSet<>(Arrays.asList(3,4,5)));
        return dxshAppServiceOrderMapper.selectList(queryWrapper);
    }

    @Override
    public Map<String, Object> deleteByOrderId(GetOrderIdRqVo getOrderIdRqVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppServiceOrder.ID,getOrderIdRqVo.getOrderId());
        DxshAppServiceOrder dxshAppServiceOrder = dxshAppServiceOrderMapper.selectOne(queryWrapper);
        dxshAppServiceOrder.setIsDelete(Constants.COMMON_FLAG_TRUE_INT);
        this.saveOrUpdate(dxshAppServiceOrder);
        return ResultMapHelper.success(ResultMsg.HAND_SUCCESS);
    }

    @Override
    public void addInfo(String suffix, Date firstInfoTime, DxshAppServiceOrder dxshAppServiceOrder, DxshAppServices dxshAppServices) {
        if(dxshAppServices == null){
            dxshAppServices = dxshAppServicesService.getById(dxshAppServiceOrder.getServiceId());
            if(dxshAppServices == null){
                return;
            }
        }
        String jobName = Constants.ORDER_LISTENER + dxshAppServiceOrder.getId() + suffix;
        String triggerName = Constants.ORDER_LISTENER + dxshAppServiceOrder.getId() + suffix;
        Map<String,Object> jobDataMap = new HashMap<>();
        jobDataMap.put("dxshAppServices",dxshAppServices);
        jobDataMap.put("dxshAppServiceOrderId",dxshAppServiceOrder.getId());
        jobDataMap.put("suffix",suffix);
        QuartzDo.setTask(DataBankOrderListenerHandler.class,jobName,triggerName,firstInfoTime,jobDataMap);
    }

    public DxshAppServiceOrder createOrder(Integer serviceId, Integer serviceNum, Integer orderType, String phone, Integer userId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppServices.ID,serviceId);
        queryWrapper.eq(DxshAppServices.IS_OFF, Constants.COMMON_FLAG_FALSE_INT);
        queryWrapper.eq(DxshAppServices.IS_DELETE,Constants.COMMON_FLAG_FALSE_INT);
        DxshAppServices dxshAppServices = dxshAppServicesService.getOne(queryWrapper);
        if(dxshAppServices == null){
            return null;
        }

        DxshAppServiceOrder dxshAppServiceOrder = new DxshAppServiceOrder();
        dxshAppServiceOrder.setOrderNo(SnowflakeIdWorkerUtils.getOrderNo());
        dxshAppServiceOrder.setServiceId(serviceId);
        dxshAppServiceOrder.setOrderType(orderType);
        dxshAppServiceOrder.setServiceNum(serviceNum);

        BigDecimal price = dxshAppServices.getPrice();//获取服务价格
        BigDecimal totalPrice = price.multiply(new BigDecimal(serviceNum));

        dxshAppServiceOrder.setTotalPrice(totalPrice);
        dxshAppServiceOrder.setBmsUserId(userId);
        dxshAppServiceOrder.setPhone(phone);
        dxshAppServiceOrder.setNewTime(LocalDateTime.now());
        dxshAppServiceOrder.setUpdateTime(LocalDateTime.now());
        dxshAppServiceOrder.setCreateWay(APP);
        this.save(dxshAppServiceOrder);
        return dxshAppServiceOrder;
    }
}
